package com.ryan.pintu.view;

import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ryan.pintu.BaseActivity;
import com.ryan.pintu.Level;
import com.ryan.pintu.R;

public class Stage extends BaseActivity{

    private Gallery mGallery;
    private LinearLayout mDot;
    private int mLastId=0;
    
    private Level mLv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.stage);
        
        mGallery = (Gallery) findViewById(R.id.gallery);
        final StageBaseAdapter adapter = new StageBaseAdapter();
        mGallery.setAdapter(adapter);
        mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ((ImageView)mDot.getChildAt(mLastId)).setImageResource(R.drawable.b_yellow);
                ((ImageView)mDot.getChildAt(position)).setImageResource(R.drawable.b_red);       
                mLastId = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                
            }
        });
        mGallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(adapter.stage<=position){
                    return ;
                }
                Intent intent = new Intent(Stage.this,Menu.class);
                intent.putExtra("stage", position);
                Stage.this.startActivity(intent);
            }
        });
        
        mDot = (LinearLayout) findViewById(R.id.dot);
        ((ImageView)mDot.getChildAt(0)).setImageResource(R.drawable.b_red);
        for(int i=1;i<Level.MAX_STAGE_NUM;i++){
            ((ImageView)mDot.getChildAt(i)).setImageResource(R.drawable.b_yellow);    
        }
        mLastId = 0;
        
    }
    
    
    @Override
    protected void onResume() {
    	super.onResume();
    	Level lv = Main.getGameLevel(Stage.this);
    	((StageBaseAdapter)mGallery.getAdapter()).stage = lv.getStage()+1;
    }
    
    private class StageBaseAdapter extends BaseAdapter {
        Bitmap[] bms;
        Bitmap lock;
        int stage=1;

        public StageBaseAdapter(){
            bms = new Bitmap[Level.MAX_STAGE_NUM];
            
            for(int i=0;i<Level.MAX_STAGE_NUM ;i++){
                try {
                	bms[i]=BitmapFactory.decodeStream(Stage.this.getAssets().open("img/"+i+"/face.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
				lock=BitmapFactory.decodeStream(Stage.this.getAssets().open("img/lock_big.png"));
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        
        @Override
        public long getItemId(int position) {
            return 0;
        }
        
        @Override
        public Object getItem(int position) {
            return null;
        }
        
        @Override
        public int getCount() {
            return Level.MAX_STAGE_NUM;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v;
            ImageView img;
            if(convertView == null){
                v = Stage.this.getLayoutInflater().inflate(R.layout.stage_item, null);
                img = (ImageView)v.findViewById(R.id.img);
                v.setTag(img);
            }else{
                v = convertView;
                img = (ImageView) v.getTag();
            }
           
            
            if(position<stage){
            	img.setImageBitmap(bms[position]);	
            }else{
            	img.setImageBitmap(lock);
            }
            
            
            return v;
        }
    };
}
